﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Visitor;

namespace ASTClass
{
    class AddExprAST : AST
    {
        private  const string TAB = "\t";
        public AddExprAST() : base() { name = "AddExpr"; }
        public AddExprAST(AST parent) : base(parent) { name = "AddExpr"; }

        public override AST Clone()
        {
            AddExprAST ast = new AddExprAST();
            ast.operators = operators;
            foreach (AST c in children)
                ast.children.Add(c.Clone());

            return ast;
        }

        public override void  CodeGen(Taste.SymbolTable st, Taste.CodeFileGen cfg)
        {
            int i = 0;
            children[i].CodeGen(st, cfg);
            foreach (Operator op in Operators)
            {

                children[++i].CodeGen(st, cfg);
                cfg.AddComment("---- Start AddExpr ----#");

                cfg.AddCommand("lw", "$v0, 0($sp)");
                cfg.AddCommand("addiu", "$sp, $sp, 4");
                cfg.AddCommand("move", "$t1, $v0");
                cfg.AddCommand("lw", "$v0, 0($sp)");
                cfg.AddCommand("addiu", "$sp, $sp, 4");
                cfg.AddCommand("move", "$t0, $v0");

                cfg.AddComment("---- AddExpr Statements ----#");

                if (op.ToString().Equals("Add"))
                {
                    cfg.AddCommand("add", "$a0, $t1, $t0");
                }
                if (op.ToString().Equals("Subtract"))
                {
                    cfg.AddCommand("sub", "$a0, $t0, $t1");
                }

                cfg.AddCommand("subu", "$sp, $sp, 4");
                cfg.AddCommand("sw", "$a0, 0($sp)");
                cfg.AddComment("---- End AddExpr ----#");
            }
        }


        #region Visitor pattern

        public override void Accept(IVisitor visitor)
        {
            visitor.Visit(this);
        }

        #endregion Visitor pattern
    }
}
